Type classes or implicit parameters? What do you prefer and why? [closed]

Posted by Petr Pudlák on Programmers See other posts from Programmers or by Petr Pudlák
Published on 2012-09-24T14:22:41Z Indexed on 2012/09/24 15:49 UTC
Read the original article Hit count: 421

I was playing a bit with Scalaz and I realized that Haskell's type classes are very similar to Scala's implicit parameters. While Haskell passes the methods defined by a type class using hidden dictionaries, Scala allows a similar thing using implicit parameters.

For example, in Haskell, one could write:

incInside :: (Functor f) => f Int -> f Int
incInside = fmap (+ 1)

and the same function using Scalaz:

import scalaz._;
import Scalaz._;

def incInside[F[_]](x: F[Int])(implicit fn: Functor[F]): F[Int] =
    fn.fmap(x, (_:Int) + 1);

I wonder: If you could choose (i.e. your favorite language would offer both), what would you pick - implicits or type classes? And what are your pros/cons?

© Programmers or respective owner

Related posts about scala

Related posts about haskell